var const_webname = "ishop-admin";
(function(global){
	//文件上传
	var FileUploader = (function(){
		var FileUploader = function(options){
			$.extend(this, options);
//			this.el = $('#' + this.id);
		}
	
		FileUploader.prototype = {
			initImg: function(img_editorId,txtUrlPathId,imgShowId,obj){
				this.img_editorId = img_editorId;
				this.txtUrlPathId = txtUrlPathId;
				this.imgShowId = imgShowId;
				this.o = obj
				this.myEditorImage= new UE.ui.Editor({initialFrameHeight:0,initialFrameWidth:0});  
				this.myEditorImage.render(img_editorId);  
				var f = this;
				this.myEditorImage.ready(function () {
					this.execCommand('serverparam', 'imageFolder', f.o.imageFolder);//返回参数值键值对的对象
					this.addListener('beforeInsertImage',function(t, arg){  
					     var rootUrl = const_rootUrl;
					     rootUrl = rootUrl.replace(const_webname,"");
					     if(f.txtUrlPathId && f.txtUrlPathId != ""){
					      	$("#"+f.txtUrlPathId).attr("value", arg[0].src);  
					 	 }
					     if(f.imgShowId && f.imgShowId != ""){
					     	$("#"+f.imgShowId).attr('src',arg[0].src);
					     }
					 });
				 });
			},
			upImg:function(txtUrlPathId,imgShowId){
				if(txtUrlPathId && txtUrlPathId != ""){
					this.txtUrlPathId = txtUrlPathId;
				}
				if(imgShowId && imgShowId != ""){
					this.imgShowId = imgShowId;
				}
				var d = this.myEditorImage.getDialog("insertimage");  
			    d.render();  
			    d.open();
			},
			initFile: function(file_editorId,filePathId,filePathNameId,obj1){
				this.file_editorId = file_editorId;
				this.filePathId = filePathId;
				this.filePathNameId = filePathNameId;
				this.o1 = obj1
				this.myEditorImage1= new UE.ui.Editor({initialFrameHeight:0,initialFrameWidth:0});  
				this.myEditorImage1.render(file_editorId);  
				var f1 = this;
				this.myEditorImage1.ready(function () {
					this.execCommand('serverparam', 'imageFolder', f1.o1.imageFolder);//返回参数值键值对的对象
					this.addListener('afterUpfile',function(t, arg){  
					     var rootUrl = const_rootUrl;
					     rootUrl = rootUrl.replace(const_webname,"");
					      	$("#"+f1.filePathId).attr("value", rootUrl+arg[0].url);  
					     	$("#"+f1.filePathNameId).attr('value',arg[0].url);
					 });
				 });
			},
			upFile:function(filePathId,filePathNameId){
				if(filePathId && filePathId != ""){
					this.filePathId = filePathId;
				}
				if(filePathNameId && filePathNameId != ""){
					this.filePathNameId = filePathNameId;
				}
				var d1 = this.myEditorImage1.getDialog("attachment");  
			    d1.render();  
			    d1.open();
			}
		};
		return new FileUploader();
	})();
	var iCitySelector = function(province,city,country){
		this.province = province;
		this.city = city;
		this.country = country;
	}
	iCitySelector.prototype = {
		init:function(){
			var theProvinceSelect = document.getElementById(this.province);
			$.ajax({
				url : const_rootUrl+"/info/area!getProvice",
				processData : false,
				data : null,
				success : function(data) {
					for ( var obj in data) {
						theProvinceSelect.options.add(new Option(data[obj].name,
								data[obj].id));
					}
				}
			});
			var o = this;
			$("#"+this.province).change(function(){
				var provinceId = $(this).val();
				var theCitySelect = document.getElementById(o.city);
				var theCountrySelect = document.getElementById(o.country);
				theCountrySelect.innerHTML = "";
				theCitySelect.innerHTML = "";
				theCountrySelect.options.add(new Option("",""));
				$.ajax({
					url : const_rootUrl+"/info/area!getCity",
					processData : false,
					data : "areaId="+provinceId,
					success : function(data) {
						for ( var obj in data) {
							theCitySelect.options.add(new Option(data[obj].name,
									data[obj].id));
						}
						//通过市获取县
						var theCountrySelect = document.getElementById(o.country);
						theCountrySelect.innerHTML = "";
							$.ajax({
								url : const_rootUrl + "/info/area!getCountry",
								processData : false,
								data : "areaId=" + $("#"+o.city).val(),
								success : function(data) {
									for ( var obj in data) {
										theCountrySelect.options.add(new Option(
												data[obj].name, data[obj].id));
									}
								}
							});
					}
				});
			});
			$("#"+this.city).change(function(){
				var cityId = $(this).val();
				var theCountrySelect = document.getElementById(o.country);
				theCountrySelect.innerHTML = "";
				$.ajax({
					url : const_rootUrl+"/info/area!getCountry",
					processData : false,
					data : "areaId="+cityId,
					success : function(data) {
						for ( var obj in data) {
							theCountrySelect.options.add(new Option(data[obj].name,
									data[obj].id));
						}
					}
				});
			});
		}
	}
	global.iCitySelector = iCitySelector;
	global.iFileUploader = FileUploader;
})(window);